/*!
 * \file __frtds_binary_to_float.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>
#include "frcomm.h"
#include "frmem.h"

#include "frbyte_buffer.h"
#include "frhex_dump.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frreport_comm.h"
#include "frct_comm.h"
#include "frtds_convert.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern
int __frtds_binary_to_float(char *p_buff
        , int i_float_type
        , char **pp_value);

static void __case1(void)
{
    char *p_buff = (char *) frmemdup("\xb8\x1e\xb3\x42", 4);
    char *p_value = NULL;
    int i_float_type = FRTDS_FLT_IEEE_HI;
    int i_ret = 0;

    i_ret = __frtds_binary_to_float(p_buff, i_float_type, &p_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_value, "89.558044");

    frfree(p_buff);
    frfree(p_value);
    return;

}

static void __case2(void)
{
    char *p_buff = (char *) frmemdup("\x1e\xb8\x42\xb3", 4);
    char *p_value = NULL;
    int i_float_type = FRTDS_FLT_VAXD;
    int i_ret = 0;

    i_ret = __frtds_binary_to_float(p_buff, i_float_type, &p_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_value, "44.779022");

    frfree(p_buff);
    frfree(p_value);
    return;
}

static void __case3(void)
{
    char *p_buff = (char *) frmemdup("\x42\xb3\x1e\xb8", 4);
    char *p_value = NULL;
    int i_float_type = FRTDS_FLT_IEEE_LO;
    int i_ret = 0;

    i_ret = __frtds_binary_to_float(p_buff, i_float_type, &p_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_value, "89.558044");

    frfree(p_buff);
    frfree(p_value);
    return;

}


static void __case4(void)
{
    char *p_buff = (char *) frmemdup("\x42\xb3\x1e\xb8", 4);
    char *p_value = NULL;
    int i_float_type = FRTDS_FLT_ND5000;
    int i_ret = 0;

    i_ret = __frtds_binary_to_float(p_buff, i_float_type, &p_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_value, "65459.117188");

    frfree(p_buff);
    frfree(p_value);
    return;
}


static void __case5(void)
{
    char *p_buff = (char *) frmemdup("\xb8\x1e\xb3\x42", 4);
    char *p_value = NULL;
    int i_float_type = 7;
    int i_ret = 0;

    i_ret = __frtds_binary_to_float(p_buff, i_float_type, &p_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_STRING_EQUAL(p_value, p_value);

    frfree(p_buff);
    return;

}


int add_suite___frtds_binary_to_float(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frtds_convert __frtds_binary_to_float()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
